package com.xyht.sca_s.student_manage_system.modules.welcomeNewStudent.util;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonCodeEnum;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonResult;
import com.xyht.sca_s.student_manage_system.common.commonReturn.ExceptionCast;
import com.xyht.sca_s.student_manage_system.modules.org.entity.SmsOrgStructure;
import com.xyht.sca_s.student_manage_system.modules.org.entity.SmsOrgUserRelation;
import com.xyht.sca_s.student_manage_system.modules.org.mapper.SmsOrgStructureMapper;
import com.xyht.sca_s.student_manage_system.modules.org.mapper.SmsOrgUserRelationMapper;
import com.xyht.sca_s.student_manage_system.modules.welcomeNewStudent.entity.resp.StudentOrgInfoResp;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

import static com.xyht.sca_s.student_manage_system.common.util.ParamCheckUtil.isNullOrEmpty;
import static com.xyht.sca_s.student_manage_system.modules.dormitory.constant.OrgLevelConstant.CLASS_LEVEL;

@Component
public class StudentUtil {

    @Resource
    private SmsOrgStructureMapper smsOrgStructureMapper;
    @Resource
    private SmsOrgUserRelationMapper smsOrgUserRelationMapper;

    /**
     *
     * @param userId userId
     * @return 返回学生的院系 专业 年级 班级信息
     */
    public StudentOrgInfoResp getStudentCollegeMajorClassInfo(String userId){
        //获取用户的组织
        List<String> orgIdList = smsOrgUserRelationMapper.selectList(new LambdaQueryWrapper<SmsOrgUserRelation>()
                .eq(SmsOrgUserRelation::getUserId, userId))
                .stream().map(SmsOrgUserRelation::getOrgId).collect(Collectors.toList());
        if (orgIdList.isEmpty()) {
            ExceptionCast.cast(CommonResult.failed(CommonCodeEnum.STUDENT_NOT_EXIST));
        }
        SmsOrgStructure smsOrgStructure = smsOrgStructureMapper.selectOne(new LambdaQueryWrapper<SmsOrgStructure>()
                .eq(SmsOrgStructure::getLevel, CLASS_LEVEL)
                .in(SmsOrgStructure::getId, orgIdList));
        Integer classOrgId = smsOrgStructure.getId();
        //班级信息
        SmsOrgStructure classOrg = smsOrgStructureMapper.selectById(classOrgId);
        if (isNullOrEmpty(classOrg)) {
           return null;
        }
        Integer classParentId = classOrg.getParentId();
        //年级信息
        SmsOrgStructure gradeOrg = smsOrgStructureMapper.selectById(classParentId);
        if (isNullOrEmpty(gradeOrg)) {
            return null;
        }
        Integer gradeParentId = gradeOrg.getParentId();
        //专业信息
        SmsOrgStructure majorOrg = smsOrgStructureMapper.selectById(gradeParentId);
        if (isNullOrEmpty(majorOrg)) {
            return null;
        }
        Integer majorParentId = majorOrg.getParentId();
        //院系信息
        SmsOrgStructure collegeOrg = smsOrgStructureMapper.selectById(majorParentId);
        if (isNullOrEmpty(collegeOrg)) {
            return null;
        }

        StudentOrgInfoResp studentOrgInfoResp = new StudentOrgInfoResp();
        studentOrgInfoResp.setCollegeId(String.valueOf(majorParentId));
        studentOrgInfoResp.setCollegeName(collegeOrg.getOrgName());
        studentOrgInfoResp.setMajorId(String.valueOf(gradeParentId));
        studentOrgInfoResp.setMajorName(majorOrg.getOrgName());
        studentOrgInfoResp.setGradeId(String.valueOf(classParentId));
        studentOrgInfoResp.setGradeName(gradeOrg.getOrgName());
        studentOrgInfoResp.setClassId(String.valueOf(classOrg.getId()));
        studentOrgInfoResp.setClassName(classOrg.getOrgName());
        return studentOrgInfoResp;
    }

}
